<?php

//记录日志
function logger($msg) {
    $line = '[ '.date('Y-m-d H:i:s').' ]'.$msg."\n\n";
    file_put_contents('phpout.log', $line, FILE_APPEND);
    echo $line;
}

//推送消息
function easyPushWechatMsg($msg) {
    $postData = json_encode([
        'appToken' => 'AT_XGmRz8RFSZBtLyaYBx52r8YziWCSrbEg',
        'content' => $msg,
        'contentType' => 1,
        'uids' => [
            'UID_ICAsgfMbgOLiDal8Zs6IYs3cF1pn',
            'UID_n1quSUwrZ0QRNkITxOeH54bYSEbK',
            'UID_e2EvnkezKPxdnQvDBPv11sdWYno3',
            'UID_wWE8tvSSFX8ZLK0F6FMFRLpD82cB',
            'UID_iIMR2zTBg42poNmVq9O2PAsA7v1V',
        ]
    ]);
    $cmdStr = 'curl \\
    -H "Content-type: application/json" \\
    -d \''.$postData.'\' \\
    http://wxpusher.zjiecode.com/api/send/message';
    exec($cmdStr, $res);
    print_r($res);
}

function getUrl($url, $headers = []) {
    $headerArray = array_merge([
        'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'Accept-Encoding: gzip, deflate, br',
        'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8',
        'Cache-Control: no-cache',
        'Connection: keep-alive',
        'Host: www.ti.com.cn',
        'Pragma: no-cache',
        'sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"',
        'sec-ch-ua-mobile: ?0',
        'sec-ch-ua-platform: "macOS"',
        'Sec-Fetch-Dest: document',
        'Sec-Fetch-Mode: navigate',
        'Sec-Fetch-Site: same-origin',
        'Sec-Fetch-User: ?1',
        'Upgrade-Insecure-Requests: 1',
        'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36',
    ], $headers);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
    $res = curl_exec($ch);
    $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    curl_close($ch);

    //头和body
    [$headerStr, $body] = explode("\r\n\r\n", $res, 2);

    $prefix = dechex(ord($body[0])) . dechex(ord($body[1]));
    $is_gzip = ('1f8b' == strtolower($prefix));
    $is_gzip and $body = gzdecode($body);

    return [$headerStr, $body];
}

function postUrl($url, $postData, $headers = []) {
    $headerArray = array_merge([
        'Accept: */*',
        'Accept-Encoding: gzip, deflate, br',
        'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8',
        'Cache-Control: no-store, must-revalidate',
        'Connection: keep-alive',
        'Expires: 0',
        'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
        'X-Sec-Clge-Req-Type: ajax',
        'Host: www.ti.com.cn',
        'sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"',
        'sec-ch-ua-mobile: ?0',
        'sec-ch-ua-platform: "macOS"',
        'Sec-Fetch-Dest: empty',
        'Sec-Fetch-Mode: cors',
        'Sec-Fetch-Site: same-origin',
        'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36',
    ], $headers);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
    $res = curl_exec($ch);
    $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    curl_close($ch);

    //头和body
    [$headerStr, $body] = explode("\r\n\r\n", $res, 2);

    $prefix = dechex(ord($body[0])) . dechex(ord($body[1]));
    $is_gzip = ('1f8b' == strtolower($prefix));
    $is_gzip and $body = gzdecode($body);

    return [$headerStr, $body];
}


